//
//  GQHomeController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/7.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQHomeController.h"
#import "GQHomeTitleView.h"
#import "GQHomeConcernController.h"
#import "GQHomeHotTopicController.h"
#import "GQHomeFamousRecommend.h"
#import "GQHomeSearchController.h"
#import "GQTabBar.h"

@interface GQHomeController () <GQHomeTitleViewDelegate, UIScrollViewDelegate>

@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) GQHomeTitleView *titleView;
@property (assign, nonatomic) NSInteger index;

@end

@implementation GQHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", self.navigationController);
    
    self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kToolBarHeight);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"home_recommend"] style:UIBarButtonItemStyleDone target:self action:@selector(famousPersonRecommend)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"home_search"] style:UIBarButtonItemStyleDone target:self action:@selector(searchItemClick)];
    
    GQHomeTitleView *GQHT = [[GQHomeTitleView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];

    GQHT.item = @[@"关注", @"热门"];
    self.navigationItem.titleView = GQHT;
    GQHT.delegate = self;
    self.titleView = GQHT;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.height)];
    scrollView.contentSize = CGSizeMake(kScreenWidth * 2, 0);
    scrollView.backgroundColor = [UIColor greenColor];
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView = scrollView;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    GQHomeConcernController *GQHCC = [[GQHomeConcernController alloc] init];
    GQHCC.view.frame = CGRectMake(0, 0, scrollView.width, scrollView.height - kNavigatationBarHeight);

    [self addChildViewController:GQHCC];
    [self.scrollView addSubview:GQHCC.view];
    GQHomeHotTopicController *GQHHTC = [[GQHomeHotTopicController alloc] init];
    GQHHTC.view.frame = CGRectMake(kScreenWidth, 0, scrollView.width, scrollView.height - kNavigatationBarHeight);
    [self addChildViewController:GQHHTC];
    [self.scrollView addSubview:GQHHTC.view];
}

- (void)viewWillAppear:(BOOL)animated
{
    [GQTabBar sharedGQTabBar].hidden = NO;
}

- (void)famousPersonRecommend
{
    GQHomeFamousRecommend *GQHFR = [[GQHomeFamousRecommend alloc] init];
    [self.navigationController pushViewController:GQHFR animated:NO];
}

- (void)searchItemClick
{
    GQHomeSearchController *GQHSC = [[GQHomeSearchController alloc] init];
    [self.navigationController pushViewController:GQHSC animated:NO];
}

#pragma mark - GQHomeTitleView的代理

- (void)homeTitleView:(GQHomeTitleView *)titleView didClick:(NSInteger)index
{
    self.scrollView.contentOffset = CGPointMake(kScreenWidth * index, -kNavigatationBarHeight);
}

#pragma mark - UIScrollView的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger pageIndex = scrollView.contentOffset.x / kScreenWidth + 0.5;
    if (_index != pageIndex) {
        [self.titleView setSelectedIndex:pageIndex];
        _index = pageIndex;
    }
}



@end
